package sparkCore

import org.apache.spark.{SparkConf, SparkContext}
import sparkCore.util.CommonTools

object Demo04_broadcastVar {

  def main(args: Array[String]): Unit = {

    val sc = CommonTools.getSparkContext("bc")

    val sign = sc.broadcast("^_^")

    val list = List("apple","banana","watermelon","apple")
    val rdd = sc.parallelize(list)

    rdd.map(line=>{
      (line+sign.value,1)
    }).reduceByKey(_+_)
      .map(kv=>{
        kv._1+":"+kv._2+sign.value
      }).foreach(println)

    println(sign.value+"=================")

    //如果想对广播变量取消持久化可以调用unpersist方法，后续如果想再次使用，会自动重新广播
//    sign.unpersist()
    //如果调用destroy方法，广播变量就不再允许使用了
    sign.destroy()
    println(sign.value+"=================")

    sc.stop()
  }

}
